项目结构
Default.aspx
Upload.aspxScripts/…style.css效果图
客户端html代码
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="UploadFile.aspx.vb" Inherits="Web.UploadFile" %>
$(function () {
var btnUpload = $('#upload');
var status = $('#status');
new AjaxUpload(btnUpload, {
action: 'Upload.aspx',
//Name of the file input box
name: 'uploadfile',
onSubmit: function (file, ext) {
if (!(ext && /^(jpg|png|jpeg|gif)$/.test(ext))) {
// check for valid file extension
status.text('Only JPG, PNG or GIF files are allowed');
return false;
}
status.text('Uploading...');
},
onComplete: function (file, response) {
//On completion clear the status
status.text('');
//Add uploaded file to list
if (response === "success") {
$('
').appendTo('#files').html('' + file).addClass('success'); } else {
$('
').appendTo('#files').text(file).addClass('error'); }
}
});
});
Upload File
- Upload File
服务端处理代码Upload.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace JqueryAjaxUploadTest
{
public partial class Upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
HttpPostedFile hpfFile = Request.Files["uploadfile"];
hpfFile.SaveAs(Server.MapPath("~/uploads/") + hpfFile.FileName);
Response.Write("success");
}
catch (Exception)
{
Response.Write("fail");
}
}
}
}
下载地址:
找到文件 jquery ajax file upload(刚刚上传,文件可能还没刷出来)